"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin
Case Name   : update user set,show errors与show warnings使用,不发生core
Create At   : 2022.08.17
@zou_jialiang0501129488
Description :
    1、创建兼容B库
    2、执行update user set，show errors，sho wwarnings
Expect      :
    1、成功
    2、update user set合理报错，show errors，sho wwarnings成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest02(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.database_name = 't_show_database_0063'

    def test_show(self):
        text = '--step1:创建兼容b库的数据库;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.database_name};" \
                  f"create database {self.database_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      result, '执行失败' + text)

        text = '--step2:执行update user set，show errors，sho wwarnings ' \
               'expect:update user set合理报错，show errors，sho wwarnings成功'
        self.log.info(text)
        sql_cmd = "update user set b = 'x' where a = 1;" \
                  "show warnings;" \
                  "show errors;"
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd,
                                               dbname=self.database_name)
        self.log.info(result)
        self.assertIn('ERROR:', result, '执行失败' + text)
        self.assertEqual(result.count("Error"), 2, '执行失败' + text)

    def tearDown(self):
        self.log.info('--清理环境--')
        result = self.primary_sh.execut_db_sql(f'drop database '
                                               f'{self.database_name};')
        self.log.info(result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      result, '清理环境失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')

